package com.fau.fm.action.index;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.fm.action.PublicAction;
import com.fau.fm.entity.MedicalAdmin;
import com.fau.fm.entity.MedicalFinancialClaim;
import com.fau.fm.entity.MedicalOrdersInfo;
import com.fau.fm.util.DateUtil;
import com.fau.fm.util.FileNameEncoderUtil;
import com.fau.fm.util.SystemConfig;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 我的对账信息Action
 * @author dangjh
 * @datetime 2014年11月21日,下午11:36:03
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class CheckingMyOrderAction extends PublicAction implements ModelDriven<MedicalFinancialClaim> {

	private static final long serialVersionUID = -5790531235930861274L;

	private MedicalFinancialClaim bean = new MedicalFinancialClaim();
	
	/**
	 * @desc 认领订单
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月21日,下午11:39:38
	 */
	@Action(value = "my_claim_order")
	public String claimOrder() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		boolean flag = this.baseAllService.getCheckingService().save(bean, admin);
		if(flag) {
			map.put("success", true);
		} else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 订单对账
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月21日,下午11:39:38
	 */
	@Action(value = "my_claim_order_checking")
	public String claimOrderChecking() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		bean.setHospitalId(admin.getEnterpriseId());
		boolean flag = this.baseAllService.getCheckingService().update(bean);
		if(flag) {
			map.put("success", true);
		} else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 待对账列表-订单对账
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月21日,下午11:39:38
	 */
	@Action(value = "wait_order_checking")
	public String waitOrderChecking() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		bean.setHospitalId(admin.getEnterpriseId());
		bean.setClaimUserId(admin.getId());
		boolean flag = this.baseAllService.getCheckingService().updateWaitClaim(bean);
		if(flag) {
			map.put("success", true);
		} else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 进入我的待对账订单页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月14日,下午4:15:34
	 */
	@Action(value = "my_pending_checking_init", results = {@Result(location = "my_pending_checking_init.jsp")})
	public String myPendingChecking(){
		return SUCCESS;
	}
	
	/**
	 * @desc 进入我的线下汇款页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月14日,下午4:15:34
	 */
	@Action(value = "my_line_remittance_init", results = {@Result(location = "my_line_remittance_init.jsp")})
	public String myLineRemittance(){
		return SUCCESS;
	}
	
	/**
	 * @desc 进入我的货到付款页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月14日,下午4:15:34
	 */
	@Action(value = "my_on_delivery_init", results = {@Result(location = "my_on_delivery_init.jsp")})
	public String myOnDelivery(){
		return SUCCESS;
	}
	
	/**
	 * @desc 加载待对账页面数据
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月21日,下午4:20:14
	 */
	@Action(value = "my_pending_checking_view")
	public String myPendingCheckingView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getCheckingService().findAllOrderClaim(map, bean, admin);
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 进入我的已对账订单页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月14日,下午4:29:32
	 */
	@Action(value = "my_audited_checking_init", results = {@Result(location = "my_audited_checking_init.jsp")})
	public String myAuditedChecking(){
		return SUCCESS;
	}
	
	/**
	 * @desc 进入我的已对账线下汇款订单页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月14日,下午4:29:32
	 */
	@Action(value = "my_audited_remittance_init", results = {@Result(location = "my_audited_remittance_init.jsp")})
	public String myAuditedRemittance(){
		return SUCCESS;
	}
	
	/**
	 * @desc 进入我的已对账货到付款订单页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月14日,下午4:29:32
	 */
	@Action(value = "my_audited_delivery_init", results = {@Result(location = "my_audited_delivery_init.jsp")})
	public String myAuditedDelivery(){
		return SUCCESS;
	}
	
	/**
	 * @desc 加载我的已对账数据
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月24日,上午10:16:49
	 */
	@Action(value = "my_audited_checking_view")
	public String myAuditedCheckingView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getCheckingService().findAllOrderReconciliation(map, bean, admin);
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 我的已对账订单信息导出
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月24日,下午5:16:01
	 */
	@Action(value = "exports_my_orders_checking")
	public String exports() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"我的已对账订单信息"+DateUtil.getDate()+".xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getCheckingService().selectedPage(bean);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 导出全部我的已对账订单信息
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月24日,下午5:17:06
	 */
	@Action(value = "exports_all_my_orders_checking")
	public String exportsAll() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			bean.setClaimUserId(user.getId());
			bean.setHospitalId(user.getEnterpriseId());
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"我的已对账订单信息"+DateUtil.getDate()+".xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getCheckingService().allPage(bean);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 批量导入我的待对账订单(货到付款)
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月27日 下午5:17:45
	 */
	@Action(value = "my_batch_checking_import", results = {@Result(name = SUCCESS, location = "my_batch_checking_import.jsp")})
	public String batchCheckingImport() {
		return SUCCESS;
	}
	
	/**
	 * @desc 批量修改我的待对账订单(货到付款)
	 * @return null
	 * @author dangjh
	 * @datetime 2014年11月27日 下午4:17:41
	 */
	@Action(value = "my_batch_checking_orders")
	public String batchChecking() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		map = new HashMap<String, Object>();
		File file = this.getImportFile(request, response);
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String info;
		try {
			info = this.baseAllService.getCheckingService().importLineDelivery(admin, file, SystemConfig.MY_WAIT_RECONCILED_CASH);
		} catch (Exception e) {
			map.put("success", false);
			map.put("message", "请导入");
			formatJSON(response, map);
			return null;
		}
		map.put("success", true);
		map.put("message", info);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 批量导入我的待对账订单(线下汇款)
	 * @return SUCCESS
	 * @author dangjh
	 * @datetime 2014年11月27日 下午5:17:45
	 */
	@Action(value = "my_batch_check_line_import", results = {@Result(name = SUCCESS, location = "my_batch_check_line_import.jsp")})
	public String batchCheckLineImport() {
		return SUCCESS;
	}
	
	/**
	 * @desc 批量修改我的待对账订单(线下汇款)
	 * @return null
	 * @author dangjh
	 * @datetime 2014年11月27日 下午4:17:41
	 */
	@Action(value = "my_batch_line_check_orders")
	public String batchLineChecking() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		map = new HashMap<String, Object>();
		File file = this.getImportFile(request, response);
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String info;
		try {
			info = this.baseAllService.getCheckingService().importLineDelivery(admin, file, SystemConfig.MY_WAIT_RECONCILED_LINE);
		} catch (Exception e) {
			map.put("success", false);
			map.put("message", "请导入");
			formatJSON(response, map);
			return null;
		}
		map.put("success", true);
		map.put("message", info);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 查看订单明细
	 * @return
	 * @author dangjh
	 * @datetime 2014年12月2日 上午9:36:05
	 */
	@Action(value = "my_order_detail_view", results = {@Result(name = SUCCESS, location = "my_order_detail_view.jsp")})
	public String myOrderDetailView() {
		return SUCCESS;
	}
	
	/**
	 * @desc 订单明细数据-订单基本信息
	 * @return null
	 * @author dangjh
	 * @datetime 2014年12月2日 上午9:38:57
	 */
	@Action(value = "order_basic_view")
	public String orderBasicView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		MedicalOrdersInfo info = this.baseAllService.getCheckingService().findById(admin, bean.getId());
		map.put("data", info);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 订单明细数据-订单产品信息
	 * @return
	 * @author dangjh
	 * @datetime 2014年12月4日 上午9:53:54
	 */
	@Action(value = "order_detail_view")
	public String orderDetailView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getCheckingService().findByOrderId(bean.getId());
		formatJSON(response, list);
		return null;
	}
	
	@Override
	public MedicalFinancialClaim getModel() {
		return bean;
	}

}
